package com.lg.partition16;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/5/9 15:21
 */
public class P1541NOIP2010提高组_乌龟棋 {

    private static final int N = 360, M = 45;
    private static int[] cnt = new int[5], g = new int[N];
    private static int[][][][] f = new int[M][M][M][M];
    private static int n, m;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); m = nextInt();
        for (int i = 1; i <= n; i++) g[i] = nextInt();
        for (int i = 1; i <= m; i++) cnt[nextInt()]++;
        f[0][0][0][0] = g[1];
        for (int i = 0; i <= cnt[1]; i++) {
            for (int j = 0; j <= cnt[2]; j++) {
                for (int x = 0; x <= cnt[3]; x++) {
                    for (int y = 0; y <= cnt[4]; y++) {
                        int val = g[1 + i + j * 2 + x * 3 + y * 4];
                        if (i > 0) f[i][j][x][y] = Math.max(f[i][j][x][y], f[i - 1][j][x][y] + val);
                        if (j > 0) f[i][j][x][y] = Math.max(f[i][j][x][y], f[i][j - 1][x][y] + val);
                        if (x > 0) f[i][j][x][y] = Math.max(f[i][j][x][y], f[i][j][x - 1][y] + val);
                        if (y > 0) f[i][j][x][y] = Math.max(f[i][j][x][y], f[i][j][x][y - 1] + val);
                    }
                }
            }
        }
        writer.println(f[cnt[1]][cnt[2]][cnt[3]][cnt[4]]);
        writer.flush();
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
